package com.yc.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.sssmpg.dto.ProductDto;
import com.yc.system.dto.Filterbook;
import com.yc.system.dto.esmodel.BookInfo;
import com.yc.system.esdao.BookInfoEsDao;
import com.yc.system.mapper.BookMapper;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import  com.yc.system.service.IBookService;


import com.yc.framework.baseClass.BaseController;
//import com.yc.framework.baseClass.ServiceFacade;
import com.yc.framework.baseClass.R;
import com.yc.system.entity.Book;
/**
 * <p>
    * 图书信息表 前端控制器
    * </p>
 *
 * @author 1850140986@qq.com
 * @since 2022-12-03
 */
@RestController
//@RequestMapping("/sys/book")
public class BookController extends BaseController
    {

    @Autowired
        //private IBookService t_bookService;
  private IBookService service;

  /**
   * 根据条件分页查询Book
   * @param model 实体对象
   * @return 查询列表
   */
     @PostMapping("/sys/book/list")
    public R list(@RequestBody Book model){
        return service.list(model);

    }

    /**
    * 根据主键查询单个对象
    * @param model 实体对象包含uuid
    * @return 成功或失败
    */
    @GetMapping("/sys/book/getById/{id}")
    public R getById(@PathVariable String id) {
        return service.getModelById(id);
    }

    /**
	 * 新增实体对象
	 *
     * @param model 实体对象
	 * @return 新增是否成功
	 */
     @PostMapping("/sys/book/save")
	public R save(@RequestBody Book model) {
		return service.saveModel(model);

	}

    /**
	 * 批量新增实体对象
	 *
     * @param modelList 实体对象列表
	 * @return 新增是否成功
	 */
	@PostMapping("/sys/book/saveBatch")
	public R saveBatch(@RequestBody List<Book> modelList) {
		return service.saveModelBatch(modelList);

	}

    /**
	 * 根据主键删除实体
	 *
     * @param uuid 实体携带主键
	 * @return 删除是否成功
	 */
	@DeleteMapping("/sys/book/removeById/{id}")
	public R removeById(@PathVariable String id) {
		return service.removeModelById(id);
	}

     /**
	 * 根据主键列表删除实体
	 *
     * @param idList 实体主键
	 * @return 删除是否成功

	@DeleteMapping("/removeByIdList")
	public R removeByIdList(@RequestBody List<Book> idList) {
        return service.removeModelByIdList(idList);

    }
 */
    /**
    * 根据主键修改实体
    *
    * @param model 等待修改的对象
    * @return 修改是否成功
    */
    @PutMapping("/sys/book/updateById")
    public R updateModelById(@RequestBody Book model) {

    	return service.updateModelById(model);

    }

    /**
    * 根据主键列表修改实体列表
    *
    * @param modelList 等待修改的对象列表
    * @return 修改是否成功
    */
    @PostMapping("/sys/book/updateByIdBatch")
    public R updateModelByIdBatch(@RequestBody List<Book> modelList) {
        return service.updateModelByIdBatch(modelList);

    }

      /**
	 * 根据多主键列表删除实体
	 *
     * @param idList 多主键
	 * @return 删除是否成功
	 */
	@DeleteMapping("/sys/book/removeByIds/{ids}")
	public R removeByIdList(@PathVariable String ids) {
        return service.removeModelByIds(ids);

    }

		@PostMapping("/sys/book/updateBybookId")
		public R updateBybookId(@RequestBody Book model) {
//			System.out.println("book.FPrice:"+model.getFPrice());
//			System.out.println("book.FPrice type="+ model.getFPrice().getClass().getName());
			if(model.getBookId()<=0){
				return service.save(model)?R.ok("更新成功"):R.err("更新失败");
			}else{
				return service.updateBybookId(model);
			}
		}

		@GetMapping("/sys/book/getBooksByUserId")
		public R getBooksByUserId(@RequestParam("userId") int userId){
		return service.selectBooksByUserId(userId);}

		@GetMapping("index/sys/book/getSwiper")
		public R getSwiper(){
		return service.getSwiper();
		}

		@GetMapping("index/sys/book/getBookByBookId")
		public R getBookByBookId(@RequestParam("bookId") int bookId){
			return service.getBookByBookId(bookId);
		}

		//分页查询
		@PostMapping("index/sys/book/getBooksByPage")
		public R getBooksByPage(@RequestBody Book findByPage){

			IPage<Book> page = new Page<>(findByPage.getPageNum() ,findByPage.getPageSize());
			QueryWrapper<Book> wrapper = new QueryWrapper<>();
//			wrapper.like("product_name","v");
			if(findByPage.getUserId()!=0){
			return service.getBooksByPageByUserId(page, wrapper, 1,findByPage.getUserId());}
			else{
				return service.getBooksByPageRandom(page, wrapper, 1);
			}

		}

		@PostMapping("index/sys/book/getBooksByTypeId")
		public R getBooksByTypeId(@RequestBody Book findByPage){
			System.out.println("----findByPage:"+findByPage.getTypeId());
		return service.getBooksByTypeId(findByPage);
		}

		@PostMapping("index/sys/book/Filterbook")
		public R Filterbook(@RequestBody Filterbook filterbook){
		return service.Filterbook(filterbook);
		}

		@PostMapping("/sys/book/pay")
		public R pay(){
		return service.pay();
		}

		@PostMapping("index/sys/book/filterBooksByES")
		public R filterBooks(@RequestBody Filterbook page){
		return service.filterBooksByES(page);
		}

		@PostMapping("sys/book/getlike")
		public R filterBooks(@RequestBody Book findByPage){
			IPage<Book> page = new Page<>(findByPage.getPageNum() ,findByPage.getPageSize());
			QueryWrapper<Book> wrapper = new QueryWrapper<>();
//			wrapper.like("product_name","v");
			return service.getlikeByPageByUserId(page, wrapper, 1,findByPage.getUserId());
		}

		@Autowired
		public BookMapper bookMapper;
		@Autowired
		public BookInfoEsDao bookInfoEsDao;

		@PostMapping("index/sys/book/refreshTES")
		public R refreshTES(){
			List<BookInfo> booksInfoList= bookMapper.booksInfoForES();
			bookInfoEsDao.saveAll(booksInfoList);
			return R.ok();
		}

		@GetMapping("sys/book/deleteById")
		public R deleteById(@RequestParam("bookId") int bookId){
			int i = bookMapper.deleteBybookId(bookId);
			if(i==1){
				return R.ok();
			}
			return R.err();
		}


}
